<template>
  <div>
    <div class="help-head">
      <h2>添加收款银行卡</h2>
      <p>您的信息将被严格保密，请放心添加</p>
    </div>
    <div class="process-main">
      <div class="login-form">
        <div class="user-info">
          <van-cell title="姓名" :value="userInfo.realse_name" size="large" />
          <van-cell title="身份证号" :value="userInfo.id_card" size="large" />
        </div>
        <div class="login-form-cont">
          <van-form>
            <van-cell-group class="cell-item" title="银行名称">
              <van-field
                class="login-form-item"
                clearable
                v-model="formData.bankName"
                ref="bankName"
                placeholder="请输入银行名称"
              >
              </van-field>
            </van-cell-group>
            <van-cell-group class="cell-item" title="银行卡号">
              <van-field
                class="login-form-item"
                clearable
                v-model="formData.bankCard"
                ref="bankCard"
                placeholder="请输入您的银行卡号"
              >
              </van-field>
            </van-cell-group>
            <van-cell-group class="cell-item" title="预留手机号">
              <van-field
                class="login-form-item"
                clearable
                v-model="formData.bankPhone"
                ref="bankPhone"
                placeholder="请输入银行预留手机号"
              >
              </van-field>
            </van-cell-group>
            <van-cell-group class="cell-item" title="短信验证码">
              <van-field
                class="login-form-item"
                clearable
                v-model="formData.smsCode"
                ref="smsCode"
                placeholder="请输入短信验证码"
              >
                <template #button>
                  <van-button  color="linear-gradient(to right, #ebb15b, #f6c775)" size="small" @click="getBankSms" type="primary">{{
                    codeText
                  }}</van-button>
                </template>
              </van-field>
            </van-cell-group>
            <van-button
              class="login-btn"
              round
              block
              type="info"
              @click="onSubmit"
              color="linear-gradient(to right, #ebb15b, #f6c775)"
            >
              完成
            </van-button>
          </van-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import {paySign, paySignSms } from '@/api/user'
const text = '获取验证码'
export default {
  computed: {
    ...mapState({
      userInfo: state => state.user.userInfo
    })
  },
  data() {
    return {
      activeNames: ['1'],
      timer: null,
      codeText: text,
      formData: {
        bankCard: undefined,
        bankName: undefined,
        bankPhone: undefined,
        smsCode: undefined
      }
    }
  },
  methods: {
    getBankSms() {
      if (this.checkData()) {
        return
      }
      if (this.codeText === text) {
        paySignSms({
          bankCard: this.formData.bankCard,
          bankPhone: this.formData.bankPhone,
          name: this.userInfo.realse_name,
          IDCard: this.userInfo.id_card
        }).then(res => {
          console.log(res)
          console.log(this.codeText)
          console.log(text)
          if (res.status !== 0) {
            this.$toast({
              position: 'bottom',
              message: res.msg
            })
            return
          }
          var time = 60
          this.timer = setInterval(() => {
            time--
            this.codeText = '（' + time + '秒）重发'
            if (time === 0) {
              clearInterval(this.timer)
              this.codeText = text
            }
          }, 1000)
        })
      }
    },
    checkData() {
      if (!this.userInfo.realse_name || this.userInfo.realse_name === '') {
        this.$utils.replace('/process')
        return true
      }
      if (!this.userInfo.id_card || this.userInfo.id_card === '') {
        this.$utils.replace('/process')
        return true
      }

      if (!this.formData.bankName) {
        this.$toast({
          position: 'bottom',
          message: '请输入银行名称'
        })
        this.$refs.bankName.focus()
        return true
      }
      if (!this.formData.bankCard) {
        this.$toast({
          position: 'bottom',
          message: '请输入您的银行卡号'
        })
        this.$refs.bankCard.focus()
        return true
      }
      if (!this.formData.bankPhone) {
        this.$toast({
          position: 'bottom',
          message: '请输入您的银行预留手机号'
        })
        this.$refs.bankPhone.focus()
        return true
      }
      return false
    },
    onSubmit() {
      if (!this.formData.bankName) {
        this.$toast({
          position: 'bottom',
          message: '请输入银行名称'
        })
        this.$refs.bankName.focus()
        return
      }
      if (!this.formData.bankCard) {
        this.$toast({
          position: 'bottom',
          message: '请输入您的银行卡号'
        })
        this.$refs.bankCard.focus()
        return
      }
      if (!this.formData.bankPhone) {
        this.$toast({
          position: 'bottom',
          message: '请输入您的银行预留手机号'
        })
        this.$refs.bankPhone.focus()
        return
      }

      if (!this.formData.smsCode) {
        this.$toast({
          position: 'bottom',
          message: '请输入短信验证码'
        })
        this.$refs.smsCode.focus()
        return
      }

      paySign({ code: this.formData.smsCode }).then(res => {
        if (res.status === 0) {
          clearInterval(this.timer)
          this.$utils.replace('/vip')
        }
        //短信验证码错误
        if (res.status === 2) {
          clearInterval(this.timer)
          this.codeText = text
        }
      })

      // authBank(this.formData).then(async res => {
      //   if (res.status == 1) {
      //     this.$utils.replace('/vip')
      //   }
      // })
    }
  }
}
</script>

<style lang="less" scoped>
::v-deep .van-hairline--top-bottom::after,
.van-hairline-unset--top-bottom::after {
  border-width: 0;
}
.help-head {
  background: #444444;
  padding: 30px 20px 40px 20px;
  h2 {
    font-size: 18px;
    color: #fff;
    margin-bottom: 10px;
  }
  p {
    font-size: 14px;
    color: #fff;
  }
}
.login-form {
  background: #fff;
  padding-top: 40px;
  border-radius: 40px 40px 0 0;
  position: relative;
  top: -24px;
  .user-info {
    width: 346px;
    margin: 0 auto;
    border-radius: 20px;
    box-shadow: 0 0 8px 2px #ccc;
    padding: 20px 0;
  }
  .login-form-cont {
    width: 346px;
    margin: 0 auto;
    .cell-item {
      border-bottom: 1px solid #eee;
      margin-bottom: 20px;
    }
    h2 {
      padding: 10px 16px;
      font-size: 26px;
      font-weight: 600;
      color: #3e4a59;
      margin-bottom: 20px;
    }
    .login-form-item {
      &.code-input {
        ::v-deep .van-field__body {
          width: 90%;
        }
      }
      .tel-icon {
        width: 20px;
        height: 20px;
      }
      .get-code {
        position: relative;
        border: none;
        background: none;
        width: auto;
        height: auto;
        color: #b9c2cd;
        &::before {
          left: -10px;
          top: 50%;
          transform: translate(0, -50%);
          position: absolute;
          content: '';
          display: inline-block;
          width: 1px;
          height: 10px;
          background: #b9c2cd;
        }
      }
    }
    .checkbox {
      padding: 10px 16px;
      display: flex;
      align-items: center;
      .checked-icon {
        width: 14px;
        height: 14px;
      }
      h3 {
        font-size: 12px;
        span {
          font-size: 12px;
          color: #f4c370;
        }
      }
    }
  }
  .login-btn {
    width: 260px;
    margin: 0 auto;
    margin-top: 30px;
  }
}
</style>
